package com.liu.controller;

import com.liu.pojo.User;
import com.liu.util.HttpServletFactory;
import com.liu.util.LoggerFactory;
import com.liu.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.sql.SQLException;

/**
 * @Author 老刘Matthew
 * @Timestamp 2022/7/13 17:11
 * @Description
 */
@WebServlet("/membership/*")
@MultipartConfig
public class MembershipServlet extends GenericServlet implements HttpServletFactory, LoggerFactory {


    public void _doAddUser(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {

        logger.info("----------> 新增 ");

        // 启动Ajax方式请求
        super.enableAjax();

        String userName = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        //获取系统时间
        User user = new User();
        user.setUser_name(userName);
        user.setPwd(password);
        user.setEmail(email);
        user.setRegister_time(new Date(System.currentTimeMillis()));
        int result = userDao.save(user);

        response.getWriter().print(gson.toJson(result));
    }

    //用户信息查询
    public void _doQueryUserInfo(HttpServletRequest request, HttpServletResponse response) {

        try {

            String user_name = getSessionUser().getUser_name();

            User user = userDao.queryByUsername(user_name);

            setSessionUser(user);
            request.getSession().setAttribute("user", user);

            new GenericServletBuilder(request, response, this.getClass()) //
                    .setUrl(Page.USER_INFO.get()) //
                    .build()
                    .getRequestMapping();

        } catch (Exception e) {
            logger.error("登陆页面异常！", e);
        }

    }

    public void _doUpdateUserInfo(HttpServletRequest request, HttpServletResponse response) throws SQLException, UnsupportedEncodingException {

        try {

            HttpSession session = request.getSession();
            User users = (User) session.getAttribute("user");
            String userName = users.getUser_name();

            String sex = request.getParameter("sex");
            String qq = request.getParameter("QQ");
            String phone = request.getParameter("mycall");
            String self_info = request.getParameter("saytext");

            User o = new User();
            o.setUser_name(userName);
            o.setSex(sex);
            o.setQq(qq);
            o.setSelf_info(self_info);
            o.setPhone(phone);

            //调用方法
            int result = userDao.update(o);

            super.setSessionUser(o);

            if (result > 0) {
                request.setAttribute("msg", "完善信息成功！");
            } else {
                request.setAttribute("msg", "完善信息失败，请重试！");
            }

            new GenericServletBuilder(request, response, this.getClass())
                    .setUrl(Page.UPDATE_INFO.get())
                    .build()
                    .getRequestMapping();

        } catch (Exception e) {
            logger.error("登陆页面异常！", e);
        }

    }

    // 修改密码操作
    public void _doUpdatePwd(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {

        try {

            HttpSession session = request.getSession();
            User users = (User) session.getAttribute("user");
            String userName = users.getUser_name();

            String password_ = request.getParameter("oldpassword");

            String password = request.getParameter("password");
            String Email = request.getParameter("email");

            User user = new User();
            user.setUser_name(userName);
            user.setPwd(password);
            user.setEmail(Email);
            user.setPhone(password_);

            int result = userDao.updatePassword(user);

            super.setSessionUser(user);

            if (result > 0) {
                request.setAttribute("msg", "修改密码成功！");
            } else {
                request.setAttribute("msg", "修改密码成功！");
            }

            new GenericServletBuilder(request, response, this.getClass())
                    .setUrl(Page.UPDATE_PWD.get())
                    .build()
                    .getRequestMapping();

        } catch (Exception e) {
            logger.error("登陆页面异常！", e);
        }
    }

    // 修改头像方法
    public void _doUpdatePortrait(HttpServletRequest request, HttpServletResponse response) throws IOException {

        try {

            String fileName = upload(request, "/portrait");

            userDao.updatePhoto(fileName, getSessionUser().getUser_id());

            new GenericServletBuilder(request, response, this.getClass()) //
                    .setUrl("_doQueryUserInfo") //
                    .build()
                    .getRequestMapping();

        } catch (Exception e) {
            logger.error("上传头像异常！", e);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {

        doPost(request, response, this.getClass());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {

        doPost(request, response, this.getClass());

    }


}
